/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.mpd.musicjukebox.ui.components;

import java.awt.Color;
import java.awt.Dimension;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author bfindeisen
 */
public class ScrollingLabel extends JLabel {
    //private String oldText;

    private java.util.Timer tmr;

    public ScrollingLabel() {
        super();
    }

    public ScrollingLabel(String initialText) {
        super(initialText);
    }

    public void scrollTheText(final String text) {
        //oldText = text;


        tmr = new java.util.Timer();
        tmr.scheduleAtFixedRate(new TimerTask() {

            //int number = 999995;
            int ctr = 1;
            //String str = " The number at the end of this line is now " + (number % 100);
            String str = text;

            @Override
            public void run() {
                try {
                    str = text.substring(0, ctr);
                    setText(str);

                    if (ctr == text.length()) {
                        tmr.cancel();
                        //str = " The number at the end of this line is now " + (number % 100);
                    }
                    ctr++;
                } catch (IndexOutOfBoundsException ex) {
                    //don't care
                }
            }
        }, 500, 100);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setPreferredSize(new Dimension(300, 300));

        JPanel p = new JPanel();

        final ScrollingLabel lbl = new ScrollingLabel();
        lbl.setBorder(BorderFactory.createLineBorder(Color.RED));
        p.add(lbl);
        frame.add(p);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        lbl.scrollTheText("small");

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ScrollingLabel.class.getName()).log(Level.SEVERE, null, ex);
                }

                lbl.scrollTheText("Bigger");

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ScrollingLabel.class.getName()).log(Level.SEVERE, null, ex);
                }

                lbl.scrollTheText("tiny");

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ScrollingLabel.class.getName()).log(Level.SEVERE, null, ex);
                }

                lbl.scrollTheText("Much Bigger that the previous three.");
            }
        }).start();

        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}
